package com.example.controller;

import com.example.common.Result;
import com.example.entity.Setmeal;
import com.example.service.SetmealService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/setmeal")
public class SetmealController {
    @Resource
     private SetmealService setmealService;

    @PostMapping
    public Result<?> save(@RequestBody Setmeal setmeal) {
        return Result.success(setmealService.save(setmeal));
    }

    @PutMapping
    public Result<?> update(@RequestBody Setmeal setmeal) {
        return Result.success(setmealService.updateById(setmeal));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        setmealService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Setmeal> findById(@PathVariable Long id) {
        return Result.success(setmealService.getById(id));
    }

    @GetMapping
    public Result<List<Setmeal>> findAll() {
        return Result.success(setmealService.list());
    }

    @GetMapping("/page")
    public Result<IPage<Setmeal>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(setmealService.page(new Page<>(pageNum, pageSize), Wrappers.<Setmeal>lambdaQuery().like(Setmeal::getName, name)));
    }

    @RequestMapping(value = "setmealSave",method = {RequestMethod.POST})
    public Result setmealSave(String itemGroupids,@RequestBody Setmeal setmeal){
        setmealService.addSetmealSave(itemGroupids,setmeal);
        return Result.success();
    }
    @RequestMapping("/setmealMore")
    public Result setmealMore(Long id){
        Setmeal setmeal = setmealService.setmealMore(id);
        return Result.success(setmeal);
    }
}